package feizhou.untils.tools;

/**
 * 
 * @ClassName : DistanceUtil.java
 * @Description: 计算两个经纬度之间的距离
 * @Modification history 2014-6-10 上午13:16:37
 * @Autor : driver.deng driver_deng@163.com
 * @Date : 2014-6-10 上午13:16:37
 * @version :
 * 
 */
public class DistanceUtil {
    private static final double EARTH_RADIUS = 6378.137;
    
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
    
    /**
     * 返回两点间的距离，单位米
     * 
     * @param lat1
     * @param lon1
     * @param lat2
     * @param lon2
     * @return
     */
    public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s =
            2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS * 1000.0;
        return s;
    }
    
    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        double miles = getDistance(22.134f, 118.123f, 0d, 0d);
        System.out.println(System.currentTimeMillis() - t1);
        System.out.println(miles);
    }
    
}
